package com.sxc.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie03")
public class Cookie03 extends HttpServlet {

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // 到期时间为负数（浏览器关闭就失效）
        Cookie cookie1 = new Cookie("username01", "zhangsan");
        cookie1.setMaxAge(-1);
        res.addCookie(cookie1);

        // 到期时间为整数（cookie存在指定时间）
        Cookie cookie2 = new Cookie("username02", "lisi");
        cookie2.setMaxAge(30);
        res.addCookie(cookie2);

        // 到期时间为0（删除cookie）
        Cookie cookie3 = new Cookie("username03", "wangwu1");
        cookie3.setMaxAge(0);
        res.addCookie(cookie3);

    }
}
